package com.An.controller;

import com.An.pojo.*;
import com.An.service.AdminService;
import com.An.service.StudentService;
import com.An.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;

/**
 * @auther AnYang
 * @date 2022/4/14 19:44
 **/
@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService service;
	@Autowired
	private AdminService adminService;

	//分页查询
	@RequestMapping("/getCourse/{sno}/{page}/{size}")
	public String getCourseBySno(@PathVariable("sno") Integer sno, @PathVariable("page") Integer page, @PathVariable("size") Integer size, Model model) {
		int number = service.getStudentCourseBySno(sno).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, size);
		List<StudentCourse> courseList = service.getStudentCourseBySno(sno);
		if (page * size >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("courseList", courseList);
		model.addAttribute("page", page);
		return "student/course";
	}

	@RequestMapping("/getTeacherByClass/{className}/{page}/{size}")
	public String getTeacherByClass(@PathVariable("className") String className, @PathVariable("page") Integer page, @PathVariable("size") Integer size, Model model) {
		int number = service.getCourseByClass(className).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, size);
		List<StuCourseInfo> teacherList = service.getCourseByClass(className);
		if (page * size >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("List", teacherList);
		model.addAttribute("page", page);
		return "student/teacherList";
	}

	@RequestMapping("/toUpdateUserInfo")
	public String toUpdateUserInfo(Model model, HttpServletRequest request) {
		Student student = (Student) request.getSession().getAttribute("student");
		System.out.println(student);
		StudentUser user = service.getUserInfoBySno(student.getSno());
		model.addAttribute("user", user);
		return "student/updateUserInfo";
	}

	@RequestMapping("/updateUserInfo")
	public String updateUserInfo(Model model, String old_id, String new_id, String old_pwd, String new_pwd) {
		StudentUser user = service.getUserInfoByUserId(old_id);
		if (user != null) {
			user.setUserid(new_id);
			user.setPassword(new_pwd);
			service.updateUserInfo(user);
			return "redirect:/homePage.html";
		} else {
			model.addAttribute("msg", "账号密码错误!");
			return "student/updateUserInfo";
		}
	}

	@RequestMapping("/showClassCourse/{class_name}/{day_id}")
	public String showClassCourseList(Model model, @PathVariable("class_name") String class_name, @PathVariable("day_id") Integer day_id) {
		List<Week> weeKList = adminService.getWeeKList();
		List<Lesson> lessonList = adminService.getLessonList();
		ClassInfo classInfo = adminService.getClassByName(class_name);
		List<ClassCourse> courseList = adminService.getClassCourseByClass(classInfo.getClassid(), day_id);
		Week week = adminService.queryDateById(day_id);
		List<ClassInfo> classInfoList = adminService.getClassInfo();
		model.addAttribute("weekList", weeKList);
		model.addAttribute("lessonList", lessonList);
		model.addAttribute("courseInfoList", courseList);
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("classInfoList", classInfoList);
		model.addAttribute("date", week);
		return "student/classCourseList";
	}

	@RequestMapping("/toShowClassCourseList")
	public String toShowClassCourseList(@RequestParam("class_name") String class_name, @RequestParam("day_id") Integer day_id) throws UnsupportedEncodingException {
		return "redirect:/student/showClassCourse/" + URLEncoder.encode(class_name, "UTF-8") + "/" + day_id;
	}

	@RequestMapping("/getStudentDormInfo/{sno}")
	public String getStudentDormInfo(@PathVariable("sno") Integer sno, Model model) {
		StudentDorm dormInfo = service.getStudentDormBySno(sno);
		if (dormInfo != null) {
			Integer dormId = dormInfo.getDorm_id();
			if (dormId != null) {
				model.addAttribute("dormId", true);
				model.addAttribute("dorm", dormInfo);
			} else {
				model.addAttribute("dormId", false);
			}
		} else {
			model.addAttribute("dormId", false);
		}
		return "student/dormInfo";
	}

	@RequestMapping("/toUpdateStuDorm/{sno}")
	public String toUpdateStuDorm(Model model, @PathVariable("sno") Integer sno) {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		List<Dormitory> dormList = adminService.getDormByGender(dorm.getDorm_gender());
		model.addAttribute("student", dorm);
		model.addAttribute("List", dormList);
		return "student/updateStuDorm";

	}

	@RequestMapping("/updateStuDorm")
	public String toAddStuDormApplication(Model model, Integer sno, String sname, String sex, Integer dorm_id, Integer bed_id, String reason) {
		StudentApplication application = new StudentApplication();
		StudentDorm stuDorm = adminService.getStuDormBySno(sno);
		Student student = adminService.getStudentBySno(sno);
		Dormitory dorm = adminService.getDormById(dorm_id);
		application.setSno(sno);
		application.setSex(sex);
		application.setSname(sname);
		application.setDorm_id(dorm_id);
		application.setReason(reason);
		application.setBed_id(bed_id);
		application.setTime(DateUtils.GetNowTime());
		application.setSign(stuDorm.getSign());
		application.setSclass(student.getSclass());
		application.setDormPlace(dorm.getDormPlace());
		System.out.println(application);
		service.addStuDormApplication(application);
		return "redirect:/homePage.html";

	}

	@RequestMapping("/getSelfExamInfo/{sno}/{exam_id}")
	public String myExamInfo(@PathVariable("exam_id") Integer exam_id, @PathVariable("sno") Integer sno, Model model) {
		List<StudentExam> stuExamSelf = adminService.getStuExamSelf(exam_id, sno);
		List<Exam> examList = adminService.getExamList();
		model.addAttribute("sno", sno);
		model.addAttribute("examList", examList);
		model.addAttribute("exam_id", exam_id);
		model.addAttribute("stuExamList", stuExamSelf);
		return "student/stuExamInfo";
	}

	@RequestMapping("/toMyExamInfo")
	public String toMyExamInfo(@RequestParam("sno") Integer sno, @RequestParam("exam_id") Integer exam_id) {
		return "redirect:/student/getSelfExamInfo/" + sno + "/" + exam_id;
	}

	@RequestMapping("/getSelfGrade/{exam_id}/{sno}")
	public String myGrade(@PathVariable("exam_id") Integer exam_id, @PathVariable("sno") Integer sno, Model model) {
		List<StudentExam> stuExamSelf = adminService.getStuExamSelf(exam_id, sno);
		List<Exam> examList = adminService.getExamList();
		model.addAttribute("examList", examList);
		model.addAttribute("exam_id", exam_id);
		model.addAttribute("stuExamList", stuExamSelf);
		return "student/studentGrade";
	}

	@RequestMapping("/toMyGradeInfo")
	public String toMyGradeInfo(@RequestParam("sno") Integer sno, @RequestParam("exam_id") Integer exam_id) {
		return "redirect:/student/getSelfGrade/" + sno + "/" + exam_id;
	}

	@RequestMapping("/ajax/checkSign")
	@ResponseBody
	public Boolean checkUpdateApplication(Integer sno) {
		StudentDorm studentDorm = service.getStudentDormBySno(sno);
		if (studentDorm != null) {
			if (studentDorm.getSign() == 2) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@RequestMapping("/judge")
	@ResponseBody
	public Boolean JudgeUserAjax(String userid) {
		StudentUser user = service.getUserInfoByUserId(userid);
		return user != null;
	}

	@RequestMapping("/judge/pwd")
	@ResponseBody
	public Boolean JudgeUserPwdAjax(String userid, String pwd) {
		StudentUser user = service.getUserInfoByUserId(userid);
		return user != null && Objects.equals(user.getPassword(), pwd);
	}

	@RequestMapping("/ajax/getCourse")
	@ResponseBody
	public Course AjaxStuCourse(String subject) {
		return service.getCourseByName(subject);
	}

	@RequestMapping("/ajax/getStuDorm")
	@ResponseBody
	public Boolean AjaxStuDorm(Integer sno) {
		StudentDorm student = service.getStudentDormBySno(sno);
		return student == null;
	}

	@RequestMapping("/ajax/addStuDorm")
	@ResponseBody
	public Boolean AjaxAddStuDorm(Integer sno, String sname, String sex) {
		StudentDorm dorm = new StudentDorm();
		dorm.setSno(sno);
		dorm.setSname(sname);
		dorm.setDorm_gender(sex);
		return service.addStudentDorm(dorm);
	}

	@RequestMapping("/ajax/judgeStuDorm")
	@ResponseBody
	public StudentDorm AjaxJudgeStuDorm(Integer sno) {
		StudentDorm stuDorm = service.getStudentDormBySno(sno);
		StudentDorm dorm = new StudentDorm();
		System.out.println(stuDorm);
		if (stuDorm != null) {
			return stuDorm;
		} else {
			//防止出现返回空值导致前端出错
			dorm.setSname("wu");
			return dorm;
		}
	}

	@RequestMapping("/ajax/quitDormApply")
	@ResponseBody
	public Boolean AjaxDeleteStuDormByIdIsNull(Integer sno) {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		if (dorm != null) {
			return service.deleteStudentDormIsNull(sno);
		} else {
			return false;
		}
	}

	@RequestMapping("/ajax/quitDorm")
	@ResponseBody
	public Boolean AjaxUpdateStuDormSign(Integer sno, Integer sign) {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		if (dorm != null) {
			return service.updateStudentDormSign(sign, sno);
		} else {
			return false;
		}
	}

	@RequestMapping("/ajax/judgeStuDormSign")
	@ResponseBody
	public Boolean AjaxCheckStuDormSign(Integer sno) {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		if (dorm != null) {
			return dorm.getSign() == 0;
		} else {
			return false;
		}
	}

	@RequestMapping("/ajax/applyReason")
	@ResponseBody
	public Boolean AjaxAddApplication(Integer sno, String reason) throws ParseException {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		Apply apply = new Apply();
		apply.setSno(sno);
		apply.setReason(reason);
		apply.setSign(dorm.getSign());
		apply.setTime(DateUtils.GetNowTime());
		return service.addApplication(apply);
	}

	@RequestMapping("/ajax/applyUpdateStuDorm")
	@ResponseBody
	public Boolean AjaxUpdateStuDormApplication(Integer sno, Integer sign, String reason) {
		StudentDorm dorm = service.getStudentDormBySno(sno);
		if (dorm != null) {
			boolean b = service.updateStudentDormSign(sign, sno);
			dorm.setSign(sign);
			Apply apply = new Apply();
			apply.setSno(sno);
			apply.setReason(reason);
			apply.setSign(dorm.getSign());
			apply.setTime(DateUtils.GetNowTime());
			boolean a = service.addApplication(apply);
			return b && a;
		} else {
			return false;
		}
	}

}